package com.example.spsaveqqty.attraction;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.spsaveqqty.R;

import java.util.List;

public class ShowCommentActivity extends AppCompatActivity {

    private ListView list_comment;
    private List<Comment> commentList;
    private MyBaseAdapt mba;//自定义适配器


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_comment);

        list_comment = findViewById(R.id.list_comment);

        Intent i = getIntent();
        commentList = (List<Comment>) i.getSerializableExtra("commentList");

        mba = new MyBaseAdapt((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE));

//3.为listView设置适配器
        list_comment.setAdapter(mba);


    }


    public class MyBaseAdapt extends BaseAdapter {
        public class ViewHolder {
            TextView tv_username;
            TextView tv_content;
            RatingBar rb_mark;
        }

        private LayoutInflater inflater;

        public MyBaseAdapt(LayoutInflater inflater) {
            this.inflater = inflater;
        }

        @Override
        public int getCount() {
            return commentList.size();
        }

        @Override
        public Object getItem(int i) {
            return commentList.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }




        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            View v = view;
            if (v == null) {
                v = inflater.inflate(R.layout.onecomment, null);
                ViewHolder vh = new ViewHolder();
                vh.tv_username = v.findViewById(R.id.tv_username);
                vh.rb_mark = v.findViewById(R.id.rb_mark);
                vh.tv_content = v.findViewById(R.id.tv_content);
                v.setTag(vh);
            }

            ViewHolder vh = (ViewHolder) v.getTag();
            Comment comment = commentList.get(i);

            double mark = comment.getStar();
            vh.tv_username.setText(comment.getUsername());
            vh.rb_mark.setRating((float)mark);
            vh.tv_content.setText(comment.getContent());
            return v;
        }
    }
}
